package com.itzjc.controller;

import com.itzjc.domain.entity.*;
import com.itzjc.domain.pojo.CurrentProjectInfo;
import com.itzjc.domain.pojo.FileFromCenter;
import com.itzjc.domain.pojo.ProjectInfo;
import com.itzjc.domain.pojo.User;
import com.itzjc.service.ProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigInteger;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 项目相关的controller
 *
 * @className: ProjectController
 * @date: 2021-06-10 21:33
 **/
@RestController
@RequestMapping("/web/api/v1/pri/project")
public class ProjectController {
    @Autowired
    private ProjectService projectService;

    /**
     * 由于token中是有id的，所以直接通过token就能有id信息了
     * 使用id信息就能直接查询，所以通过token就能得到想要的所有东西
     * 由于token在拦截器中解析了，所以需要从request获取
     *
     * @return:
     **/

    @GetMapping("/getProjectByToken")
    public Result getProjectByToken(HttpServletRequest request) {
        Integer userId = (Integer) request.getAttribute("user_id");
        if (null == userId) {
            return Result.buildError("用户没有项目", 300);
        }
        List<ProjectInfo> projectInfoList = projectService.getProjectByToken(userId);
        return Result.buildSuccess("获取列表成功", projectInfoList);
    }

    @GetMapping("/getFilesFromCenter")
    public Result getFilesFromCenter(HttpServletRequest request) {
        Integer userId = (Integer) request.getAttribute("user_id");
        if (null == userId) {
            return Result.buildError("用户没有项目", 300);
        }
        List<FileFromCenter> fileFromCenter = projectService.getFilesFromCenter(userId);
        return Result.buildSuccess("获取列表成功", fileFromCenter);
    }


    @PostMapping("/createProjectById")
    public Result createProjectById(@RequestBody RequestProject requestProject) {
        //因为携带的信息太多了，就没有定义为私有的方法了，直接就是放开的，所以没有byToken而是直接ByID
        String projectName = projectService.createProjectById(requestProject);
        if (null == projectName) {
            return Result.buildError("请不要重复创建", 300);
        }
        return Result.buildSuccess("创建项目成功", projectName);
    }

    @PostMapping("/uploadFileToCenter")
    public Result uploadFileToCenter(@RequestBody RequestFileCenterCreator requestFileCenterCreator) {
        //因为携带的信息太多了，就没有定义为私有的方法了，直接就是放开的，所以没有byToken而是直接ByID
        String fileName = projectService.uploadFileToCenter(requestFileCenterCreator);
        if (null == fileName) {
            return Result.buildError("上传失败", 300);
        }
        return Result.buildSuccess("上传成功", fileName);
    }

    @PostMapping("/rejectCurrentProject")
    public Result rejectCurrentProject(@RequestBody RequestRejectCurrentProject requestRejectCurrentProject) {
        //因为携带的信息太多了，就没有定义为私有的方法了，直接就是放开的，所以没有byToken而是直接ByID
        String projectName = projectService.rejectCurrentProject(requestRejectCurrentProject);
        if (null == projectName) {
            return Result.buildError("请不要重复创建", 300);
        }
        return Result.buildSuccess("创建项目成功", projectName);
    }

    @PostMapping("/sureCurrentProjectStage")
    public Result sureCurrentProjectStage(@RequestBody RequestSureCurrentProjectStage requestSureCurrentProjectStage) {
        //因为携带的信息太多了，就没有定义为私有的方法了，直接就是放开的，所以没有byToken而是直接ByID
        String projectName = projectService.sureCurrentProjectStage(requestSureCurrentProjectStage);
        if (null == projectName) {
            return Result.buildError("请不要重复创建", 300);
        }
        return Result.buildSuccess("创建项目成功", projectName);
    }

    @GetMapping("/deleteProjectByToken")
    public Result deleteProjectByToken(HttpServletRequest request) {
        Integer userId = (Integer) request.getAttribute("user_id");
        String info = (String) request.getAttribute("info");
        Integer projectId = Integer.valueOf(info);
        if (null == userId) {
            return Result.buildError("查询不到项目", 300);
        }
        projectService.deleteProjectByToken(userId, projectId);
        return Result.buildSuccess("删除成功");
    }

    @GetMapping("/generateForgetCode")
    public Result generateForgetCode(HttpServletRequest request) {
        Integer userId = (Integer) request.getAttribute("user_id");
        String info = (String) request.getAttribute("info");

        if (null == userId) {
            return Result.buildError("查询不到项目", 300);
        }
        projectService.generateForgetCode(userId, info);
        return Result.buildSuccess("生成成功");
    }

    @GetMapping("/deleteFileFromCenterByToken")
    public Result deleteFileFromCenterByToken(HttpServletRequest request) {
        Integer userId = (Integer) request.getAttribute("user_id");
        String info = (String) request.getAttribute("info");
        Integer fileId = Integer.valueOf(info);
        if (null == userId) {
            return Result.buildError("查询不到文件", 300);
        }
        projectService.deleteFileFromCenterByToken(userId, fileId);
        return Result.buildSuccess("删除成功");
    }

    @GetMapping("/recoverUser")
    public Result recoverUser(HttpServletRequest request) {
        Integer userId = (Integer) request.getAttribute("user_id");
        String info = (String) request.getAttribute("info");
        Integer uIdForUser = Integer.valueOf(info);
        if (null == userId) {
            return Result.buildError("查询不到该用户", 300);
        }
        projectService.recoverUser(userId,uIdForUser);
        return Result.buildSuccess("删除成功");
    }

    @GetMapping("/moveOutUser")
    public Result moveOutUser(HttpServletRequest request) {
        Integer userId = (Integer) request.getAttribute("user_id");
        String info = (String) request.getAttribute("info");
        Integer uIdForUser = Integer.valueOf(info);
        if (null == userId) {
            return Result.buildError("查询不到该用户", 300);
        }
        projectService.moveOutUser(userId, uIdForUser);
        return Result.buildSuccess("删除成功");
    }

    @GetMapping("/deleteUser")
    public Result deleteUser(HttpServletRequest request) {
        Integer userId = (Integer) request.getAttribute("user_id");
        String info = (String) request.getAttribute("info");
        Integer uIdForUser = Integer.valueOf(info);
        if (null == userId) {
            return Result.buildError("查询不到该用户", 300);
        }
        projectService.deleteUser(userId, uIdForUser);
        return Result.buildSuccess("删除成功");
    }

    @GetMapping("/okForThisProject")
    public Result okForThisProject(HttpServletRequest request) {
        Integer userId = (Integer) request.getAttribute("user_id");
        String info = (String) request.getAttribute("info");
        Integer projectId = Integer.valueOf(info);
        if (null == userId) {
            return Result.buildError("查询不到项目", 300);
        }
        projectService.okForThisProject(projectId);
        return Result.buildSuccess("删除成功");
    }

    @GetMapping("/getAuditProjectByToken")
    public Result getOpeningProjectByToken(HttpServletRequest request) {
        Integer userId = (Integer) request.getAttribute("user_id");
        if (null == userId) {
            return Result.buildError("用户没有项目", 300);
        }
        List<ProjectInfo> projectInfoList = projectService.getAuditProjectByToken(userId);
        return Result.buildSuccess("获取列表成功", projectInfoList);
    }

    @GetMapping("/getAuditedProjectByToken")
    public Result getOpenedProjectByToken(HttpServletRequest request) {
        Integer userId = (Integer) request.getAttribute("user_id");
        if (null == userId) {
            return Result.buildError("用户没有项目", 300);
        }
        List<ProjectInfo> projectInfoList = projectService.getAuditedProjectByToken(userId);
        return Result.buildSuccess("获取列表成功", projectInfoList);
    }

    @GetMapping("/getBlockedUsers")
    public Result getBlockedUsers(HttpServletRequest request) {
        Integer userId = (Integer) request.getAttribute("user_id");
        if (null == userId) {
            return Result.buildError("用户没有项目", 300);
        }
        List<User> userList = projectService.getBlockedUsers(userId);
        return Result.buildSuccess("获取列表成功", userList);
    }

    @GetMapping("/getUnBlockedUsers")
    public Result getUnBlockedUsers(HttpServletRequest request) {
        Integer userId = (Integer) request.getAttribute("user_id");
        if (null == userId) {
            return Result.buildError("用户没有项目", 300);
        }
        List<User> userList = projectService.getUnBlockedUsers(userId);
        return Result.buildSuccess("获取列表成功", userList);
    }

    /**
    * 获取用户选择的项目的详情
    * @param request
    * @return:
    **/

    @GetMapping("/getProjectInfoByTokenAndId")
    public Result getProjectInfoByTokenAndId(HttpServletRequest request) {
        //这里好像用不到这个uid吧？
        Integer userId = (Integer) request.getAttribute("user_id");
        String info = (String) request.getAttribute("info");
        Integer projectId = Integer.valueOf(info);
        if (null == userId || null == projectId) {
            return Result.buildError("请重新登录，再尝试", 300);
        }
        CurrentProjectInfo currentProjectInfo = projectService.getProjectInfoByTokenAndId(userId, projectId);
        return Result.buildSuccess("查询成功", currentProjectInfo);
    }


    /**
    * 搜索的时候只用一个参数，并且验证登录就行了，所以还是参照查找单个项目的思路
    * @param request
    * @return:
    **/

    @GetMapping("/searchProjectInfoByTokenAndString")
    public Result searchProjectInfoByTokenAndString(HttpServletRequest request) {
        //这里好像用不到这个uid吧？
        Integer userId = (Integer) request.getAttribute("user_id");
        String word = (String) request.getAttribute("info");
        if (null == userId ) {
            return Result.buildError("请重新登录，再尝试", 300);
        }
        List<ProjectInfo> projectInfoList = projectService.searchProjectInfoByTokenAndString(userId, word);
        return Result.buildSuccess("查询成功", projectInfoList);
    }

    /**
     * 搜索的时候只用一个参数，并且验证登录就行了，所以还是参照查找单个项目的思路
     * @param request
     * @return:
     **/

    @GetMapping("/searchUsersByTokenAndString")
    public Result searchUsersByTokenAndString(HttpServletRequest request) {
        //这里好像用不到这个uid吧？
        Integer userId = (Integer) request.getAttribute("user_id");
        String word = (String) request.getAttribute("info");
        if (null == userId ) {
            return Result.buildError("请重新登录，再尝试", 300);
        }
        List<User> projectInfoList = projectService.searchUsersByTokenAndString(userId, word);
        return Result.buildSuccess("查询成功", projectInfoList);
    }



    /**
     * 由于需要两个数据，所以使用post，并且需要在拦截器中开启访问权限
     *
     * @param
     * @return:
     **/

    @PostMapping("/updateProjectImg")
    public Result updateProjectImg(@RequestBody RequestImage requestImage) {
        String message = projectService.updateProjectImg(requestImage);
        if (null != message) {
            String img = requestImage.getImgName();
            return Result.buildSuccess(message, img);
        }
        return Result.buildError("更新失败", 300);
    }

    /**
     * 有两个参数需要修改，而且还需要项目id，所以就用post，使用post就要开放权限，本次没有使用这个功能
     *
     * @param requestModule
     * @return:
     **/

    @PostMapping("/updateProjectModule")
    public Result updateProjectModule(@RequestBody RequestModule requestModule) {
        String message = projectService.updateProjectModule(requestModule);
        if (null != message) {
            Map<String, String> map = new HashMap<>();
            map.put("objName", requestModule.getObjName());
            map.put("mtlName", requestModule.getMtlName());

            return Result.buildSuccess(message, map);
        }
        return Result.buildError("更新失败", 300);
    }


    /**
     * 同样需要开放访问权限
     *
     * @param requestUpdateAllProject
     * @return:
     **/

    @PostMapping("/updateProjectById")
    public Result updateWholeProject(@RequestBody RequestUpdateAllProject requestUpdateAllProject) {
        //不用返回任何数据，如果需要数据，就让前端自己获取就行了
        projectService.updateWholeProject(requestUpdateAllProject);
        return Result.buildSuccess("信息修改成功");
    }


    @GetMapping("/findByIdForSecondSource")
    public Result findByIdForSecondSource(int id){
        List<BigInteger> Did =projectService.findByIdForSecondSource(id);
        return Result.buildSuccess("多数据库配置成功！",Did);

    }



}


